import { _decorator, Component, director, find, instantiate, Node, Prefab } from "cc";
import { TLWLSJ_Panel } from "./Panel";
import { BundleManager, Bundles } from "../AshFramework/Managers/BundleManager";
import { BlankScreen } from "./BlankScreen";
import { EventManager, MyEvent } from "../AshFramework/Managers/EventManager";
import PlayerController from "../PlayerController";
import { ZTool } from "../AshFramework/Utils/ZTool";
import { GameMapName, MAP } from "../AshFramework/Datas/Constant";
import { PrefsManager } from "../AshFramework/Managers/PrefsManager";
import { MapController } from "./MapController";
import { TLWLSJ_UIManager } from "./UIManager";
import { AudioManager, Audios } from "../AshFramework/Managers/AudioManager";
import { TipsController } from "./TipsController";
const { ccclass, property } = _decorator;

@ccclass("GameManager")
export class GameManager extends Component {
  public static Instance: GameManager = null;

  @property(Node)
  BackpackPanel: Node = null;

  @property(Node)
  ShopPanel: Node = null;

  @property(Node)
  Canvas: Node = null;

  @property(Node)
  BulletLayout: Node = null;

  @property(Node)
  EnemyLayout: Node = null

  @property(Node)
  MapLayout: Node = null

  @property(Node)
  PackButton: Node = null;

  IsRecharge: boolean = false;
  IsPause: boolean = false;
  private _isClick: boolean = false;

  protected onLoad(): void {
    GameManager.Instance = this;
  }

  protected start(): void {
    this.loadMap("", true);
  }

  backpackBtn() {
    AudioManager.PlaySound(Audios.Open);
    this.IsPause = true;
    this.BackpackPanel.active = true;
    if (TipsController.Instance.IsPack && !this._isClick && TipsController.Instance.IsPackStart) {
      this._isClick = true;
      TipsController.Instance.nextTips2D(0);
    }
  }

  closeBackpack() {
    AudioManager.PlaySound(Audios.Open);
    this.IsPause = false;
    this.close(this.BackpackPanel);
    if (TipsController.Instance.IsPack && TipsController.Instance.IsPackStart) {
      TipsController.Instance.nextTips2D(4);
    }
  }

  shopBtn() {
    AudioManager.PlaySound(Audios.Open);
    this.IsPause = true;
    this.ShopPanel.active = true;
  }

  closeShop() {
    AudioManager.PlaySound(Audios.Open);
    this.IsPause = false;
    this.close(this.ShopPanel);
    EventManager.Scene.emit(MyEvent.UPDATESLD);
  }

  close(panel: Node) {
    panel.getComponent(TLWLSJ_Panel).close(() => {
      panel.active = false;
    });
  }

  show() {
    BundleManager.LoadPrefab(Bundles.Prefabs, "过渡黑屏").then(
      (prefab: Prefab) => {
        const node: Node = instantiate(prefab);
        node.parent = this.Canvas;
        node.getComponent(BlankScreen).show();
      }
    );
  }

  loadMap(name: string = "", isFirst: boolean = false) {
    if (!isFirst) {
      BundleManager.LoadPrefab(Bundles.Prefabs, "加载中").then((prefab: Prefab) => {
        const panel = instantiate(prefab);
        panel.parent = this.Canvas;
      })
    }
    if (name === "") name = ZTool.GetEnumKeyByValue(MAP, PrefsManager.Instance.userData.CurMap);
    BundleManager.LoadPrefab(Bundles.Prefabs, name).then((prefab: Prefab) => {
      this.EnemyLayout.removeAllChildren();
      const map = instantiate(prefab);
      this.MapLayout.removeAllChildren();
      map.parent = this.MapLayout;
      PlayerController.Instance.node.setWorldPosition(map.getComponent(MapController).getPlayerInitPos());
      if (MapController.Instance.IsMAP1) {
        PlayerController.MAXHP = 9999;
        PlayerController.Instance.CurHp = PlayerController.MAXHP;
      } else {
        PlayerController.MAXHP = 1000;
      }
    })
  }

  nextMap() {
    PrefsManager.Instance.userData.CurMap = ZTool.getAdjacentEnumCirculation(MAP, PrefsManager.Instance.userData.CurMap);
    PrefsManager.Instance.saveData();
    this.loadMap();
  }

  startGame() {
    this.IsPause = false;
    const gameMapName: string = GameMapName[PrefsManager.Instance.userData.CurMap];
    this.loadMap(gameMapName);
  }

  gameFail() {
    this.IsPause = true; ''
    this.loadMap();
    PlayerController.Instance.cure();
  }
}
